package com.tanhua.dubbo.api;

import com.tanhua.model.db.UserLike;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class UserLikeToApiImpl implements UserLikeToApi {


    @Autowired
    private MongoTemplate mongoTemplate;
    //添加喜欢人
    @Override
    public void save(UserLike userLike) {
        mongoTemplate.save(userLike);
    }
    //查询是否互相喜欢
    public Boolean find(Long userId, Long likeUserId,String isLike) {
        Criteria criteria = Criteria.where("userId").is(likeUserId)
                .and("likeUserId").is(userId)
                .and("isLike").is(isLike);
        boolean exists = mongoTemplate.exists(Query.query(criteria), UserLike.class);
        System.out.println("exists:"+exists);
        if(exists){
            return true;
        }
        return false;
    }
    //根据用户id查询likeUser
    public List<UserLike> findAll(Long UserId) {
        Criteria criteria1 = Criteria.where("userId").is(UserId);
        List<UserLike> likes = mongoTemplate.find(Query.query(criteria1), UserLike.class);
        return likes;
    }
    //根据用户id查询喜欢
    @Override
    public List<UserLike> findLoveAll(Long userId, String isLike) {
        Criteria criteria = Criteria.where("userId").is(userId).and("isLike").is(Boolean.valueOf(isLike));
        List<UserLike> userLikeTos = mongoTemplate.find(Query.query(criteria), UserLike.class);
        System.out.println("喜欢："+userLikeTos);
        return userLikeTos;
    }
    //根据用户id查询粉丝
    @Override
    public List<UserLike> findFanAll(Long userId, String isLike) {
        // System.out.println(userId+"---"+isLike);
        Criteria criteria1 = Criteria.where("likeUserId").is(userId).and("isLike").is(Boolean.valueOf(isLike));
        List<UserLike> likes = mongoTemplate.find(Query.query(criteria1), UserLike.class);
        return likes;
    }
    public List<UserLike> findFansiAll(Long userId  ) {
        // System.out.println(userId+"---"+isLike);
        Criteria criteria1 = Criteria.where("likeUserId").is(userId);
        List<UserLike> likes = mongoTemplate.find(Query.query(criteria1), UserLike.class);
        return likes;
    }
    //查询是否存在
    @Override
    public Boolean findExist(Long userId, Long likeUserId) {
        Criteria criteria = Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId);
        return mongoTemplate.exists(Query.query(criteria),UserLike.class);

    }
    //修改喜欢状态
    @Override
    public void update(UserLike userLike) {
        Criteria criteria = Criteria.where("userId").is(userLike.getUserId())
                .and("likeUserId").is(userLike.getLikeUserId());
        Update update = new Update();
        update.set("isLike",userLike.getIsLike());
        mongoTemplate.updateFirst(Query.query(criteria),update,UserLike.class);
    }
    //查询喜欢用户
    public UserLike findeachLove(Long userId, Long likeUserId, String isLike) {
        Criteria criteria = Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId)
                .and("isLike").is(Boolean.valueOf(isLike));
        return mongoTemplate.findOne(Query.query(criteria),UserLike.class);
    }
}
